package cn.momoshenchi.product.controller;

import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.momoshenchi.product.entity.AttrEntity;
import cn.momoshenchi.product.service.AttrAttrgroupRelationService;
import cn.momoshenchi.product.service.AttrService;
import cn.momoshenchi.product.service.CategoryService;
import cn.momoshenchi.product.vo.AttrGroupRelationVo;
import cn.momoshenchi.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.momoshenchi.product.entity.AttrGroupEntity;
import cn.momoshenchi.product.service.AttrGroupService;
import cn.momoshenchi.common.utils.PageUtils;
import cn.momoshenchi.common.utils.R;



/**
 * 属性分组
 *
 * @author ${author}
 * @email ${email}
 * @date 2021-04-23 22:04:06
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;
    /**
     *
     */
    @GetMapping("/{categoryId}/withattr")
    public R getAttrgroupwithattr(@PathVariable Long categoryId){
           List<AttrGroupWithAttrsVo>attrGroupWithAttrsVos= attrGroupService.getAttrGroupWithAttrsByCategoryId(categoryId);
        return R.ok().put("data",attrGroupWithAttrsVos);
    }
    /**
     * 新增分组
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo>vos){
        relationService.saveBatch(vos);
        return R.ok();
    }
    /**
     * 关联查询
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable  Long attrgroupId){
        List<AttrEntity> list= attrService.getRelationAttr(attrgroupId);

        return R.ok().put("data", list);
    }
    /**
     *  非关联查询
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R noattrRelation(@PathVariable  Long attrgroupId,
                            Map<String,Object>params){
        PageUtils pageUtils=attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page", pageUtils);
    }


    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody  AttrGroupRelationVo []vo){
         attrService.removeRelation(vo);

        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 列表/catId
     */
    @RequestMapping("/list/{catId}")
    public R listid(@RequestParam Map<String, Object> params,
    @PathVariable Long catId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long id = attrGroup.getCategoryId();
        Long[]path=categoryService.findCategoryPath(id);
        attrGroup.setCategoryPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
